
#import "WebAnalyticsBrowserView.h"
#import "BGOSessionEventsConstants.h"
#import "BGOAnalyticsManager.h"

@interface WebAnalyticsBrowserView ()

-(void)setPageView:(NSString *)filename;

@end

@implementation WebAnalyticsBrowserView
@synthesize contentView,webViewAnalytics;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
    contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
	self.view = contentView;
    
    webViewAnalytics=[[UIWebView alloc]initWithFrame:contentView.frame];
    webViewAnalytics.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    webViewAnalytics.delegate=self;
    [self.contentView addSubview:webViewAnalytics];

}
- (void)loadHTML:(NSString*)fileName {
    
    self.navigationItem.title=fileName;
    
    if ([fileName isEqualToString:@"Bango"]) {
        [webViewAnalytics loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.bango.com"]]];
    }
    else
    {
        [webViewAnalytics loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:fileName ofType:@"html"]]]];
    }
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [super viewWillDisappear:animated];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark- WebView Delegate

- (BOOL) webView:(UIWebView*)wbView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
    NSString *filename=[webViewAnalytics stringByEvaluatingJavaScriptFromString:@"document.title"];
   
    [self setPageView:filename];
   
}
#pragma mark- Set Page View
-(void)setPageView:(NSString *)filename
{
    NSDictionary *eventParameters = [[NSDictionary alloc] initWithObjectsAndKeys: @"tommy page view emulation", @"BGO_CAMPAIGN", @"tommy custom value", @"BGO_CUSTOM1", nil];
    
    [[BGOAnalyticsManager sharedManager] onPageViewWithEventParameters:eventParameters eventDetail:@"test app detail - emulating page view" eventValue:filename];
}
#pragma mark - Memory management


-(void)dealloc
{
    [contentView release];
    [webViewAnalytics release];
    [super dealloc];
}
@end
